package com.rtx.wms.std.transer.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rtx.api.annotation.DataSourceAnnotation;
import com.rtx.wms.std.inv.bean.Lotattribute;
import com.rtx.wms.std.inv.service.LotattributeService;
import com.rtx.wms.std.transer.bean.Transfer;
import com.rtx.wms.std.transer.bean.TransferDetail;
import com.rtx.wms.std.transer.mapper.TrnsferMapper;
import com.rtx.wms.std.transer.service.TransferDetailService;
import com.rtx.wms.std.transer.service.TransferService;

@Service
public class TransferServiceImpl extends ServiceImpl<TrnsferMapper,Transfer> implements TransferService{

	@Autowired
	TransferDetailService trnsferDetailService;
	
	@Autowired
	LotattributeService lotattributeService;
	
	@Transactional
	@DataSourceAnnotation
	@Override
	public void insertTrnsferAndDetail(String datasourckey, Transfer trnsfer) {
		for(TransferDetail td:trnsfer.getDetails()) {
			trnsferDetailService.save(td);
		}
		this.save(trnsfer);
	}

	@DataSourceAnnotation
	@Override
	public List<Transfer> queryByCondition(String datasourckey, Wrapper<Transfer> query) {
		return this.list(query);
	}

	@DataSourceAnnotation
	@Override
	public List<Transfer> queryWithDetailByCondition(String datasourcekey, Wrapper<Transfer> query) {
		List<Transfer> list = this.list(query);
		for(Transfer t:list) {
			LambdaQueryWrapper<TransferDetail> tdqw = Wrappers.lambdaQuery();
			tdqw.eq(TransferDetail::getTransferkey, t.getTransferkey());
			List<TransferDetail> detailList = trnsferDetailService.list(tdqw);
			for(TransferDetail td:detailList) {
				LambdaQueryWrapper<Lotattribute> lquery = Wrappers.lambdaQuery();
				lquery.eq(Lotattribute::getLot, td.getFromlot());
				lquery.eq(Lotattribute::getSku, td.getFromsku());
				lquery.eq(Lotattribute::getStorerkey, td.getFromstorerkey());
				List<Lotattribute> lList = lotattributeService.list(lquery);
				if(CollectionUtils.isEmpty(lList)) {
					continue;
				}
				td.setFromlottable01(lList.get(0).getLottable01());
				td.setFromlottable02(lList.get(0).getLottable02());
				td.setFromlottable03(lList.get(0).getLottable03());
				td.setFromlottable04(lList.get(0).getLottable04());
				td.setFromlottable05(lList.get(0).getLottable05());
				td.setFromlottable06(lList.get(0).getLottable06());
				td.setFromlottable07(lList.get(0).getLottable07());
				td.setFromlottable08(lList.get(0).getLottable08());
				td.setFromlottable09(lList.get(0).getLottable09());
				td.setFromlottable10(lList.get(0).getLottable10());
			}
			t.setDetails(detailList);
		}
		return list;
	}

	@DataSourceAnnotation
	@Override
	public void updStatus(String datasourcekey, Wrapper<Transfer> upd) {
		this.update(upd);
	}
}
